<?php
class Wolf
{
	/*
	 * The version info!
	 */
	const VERSION = '1.0.0809';
	/*
	 * Sent some suggest,and your name will show at the following!
	 */
	const DEVGROUP ='snowwolf[jidixuelang@gmail.com]';
	/*
	 * Set the base system-config!
	 */
	public static function Init()
	{
		error_reporting ( SYS_ERROR_REPORTING );
		date_default_timezone_set ( SYS_TIMEZONE_SET );
		set_magic_quotes_runtime ( SYS_MAGIC_QUOTES );
	    self::ClearInput();
	    self::CreateBase();
	}
	
	public static function LoadModule()
	{
		extract( $GLOBALS );
		$module = isset ( $module )? $module : SYS_DIRECTORY_INDEX;
		$GLOBALS['module'] = $module = array_map ( 'strtolower', explode ('-',$module) );
		$modulePath = DIR_MODULE . implode ( '/', $module ) . '.module.php'; 
		echo !file_exists( $modulePath );
		
		if( !file_exists( $modulePath ) )
		{
			self::Error( "Module is not Exist!");
		}
		include ( $modulePath );
	}
	
	public static function LoadController( $controllerName )
	{
		$controllerFile = DIR_CONTROLLER. $controllerName .'.controller.php';

		if ( !file_exists( $controllerFile ) )
		{
			self::Error( "Controller is not Exist!" );
		}
		include_once ( $controllerFile );
		$controllerName = firstBigChar ( $controllerName ).'Controller';
		array_shift ( $arguments = func_get_args () );
		return new $controllerName( $arguments );
	}
	
	
	public static function LoadClass( $className )
	{
		$classFile = DIR_CLASS . $className .'.class.php';
		if ( !file_exists( $classFile ) )
		{
			die ( "Class is not Exist!" );
		}
		include_once ( $classFile );
		$className = firstBigChar ( $className );
		array_shift ( $arguments = func_get_args () );
		return new $className( $arguments );
	}
	
	public static function LoadInstance( $className )
	{
		$classFile = DIR_CLASS . $className .'.class.php';
		if ( !file_exists ( $classFile ) )
		{
			die ( "Class is not Exist!" );
		}
		include_once ( $classFile );
		$className = firstBigChar ( $className );
		array_shift ( $arguments = func_get_args () );
	 	@eval("\$instance =  $className::getInstance(\$arguments);");
	 	return $instance;
	}
	
	
	public static function LoadModel( $modelName )
	{
		$modelFile = DIR_MODEL . $modelName .'.model.php';
		if ( !file_exists ( $modelFile ) )
		{
			die ( "Model is not Exist!" );
		}
		include_once ( $modelFile );
		$modelName = firstBigChar ( $modelName ).'Model';
		array_shift ( $arguments = func_get_args () );
	 	@eval("\$model =  $modelName::getInstance(\$arguments);");
	 	return $model;
	}
	
	public static function LoadView( $viewPath )
	{
		$tpl = $GLOBALS['tpl']; 
		$view = Wolf::LoadInstance( 'view' );
		$view->assign( $tpl );
		$view->display( $viewPath );
	}
	/**
	 * Show error info!
	 * 
	 * As $category == 0 ,$sign is the error-sign,which recode at '/core/*.language.php'
	 *
	 * @param string $sign
	 * @param int $category
	 */
	public static function Error( $info, $category=null )
	{
		
		header ( 'content-type:text/html; charset=utf-8' );
		switch($category){
			case 'REDIRECT':
				$args = func_get_args ();
				$url = $args[2];
				echo $info.'<a href="'.$url.'">5秒后未自动跳转，请点击！</a>';
				exit ();
				break;
			default:
				exit ( $info.'<a href="'
						.( isset ( $_SERVER['HTTP_REFERER'] )?$_SERVER['HTTP_REFERER']:'' )
						.'">5秒后未自动跳转，请点击！</a>'
					 );
				break;
		}
		exit;
	}
	/*
	public static function GetInstance($instanceName)
	{
		$instace = null;
		switch($instanceName){
			case 'db':
				require_once(DIR_LIB.'db/mysql.php');
				$instace = Mysql::getInstance();
				break;
			default:
				$instace = false;
				break;
		}
		return $instace;
	}
	*/
	
	private static function ClearInput()
	{
		( isset ( $_REQUEST['GLOBALS'] ) || isset ( $_FILES['GLOBALS'] ) ) && die ( 'System assert error!' );
		foreach ( array ( '_COOKIE', '_POST', '_GET' ) as $_request ) {
			foreach ( $GLOBALS[$_request] as $_key => $_value ) {
				$_key{0} != '_' && $GLOBALS[$_key] = wolfAddslashes($_value);
			}
		}
		unset( $_GET ,$_POST, $_COOKIE );
	}
	private static function CreateBase()
	{
		$GLOBALS['auth'] = Wolf::LoadModel( 'auth' );
		$GLOBALS['auth']->loginByCookie();
		$GLOBALS['tpl'] = array();
		$GLOBALS['tpl']['auth'] = $GLOBALS['auth']->info;
	}
}
?>